package com.caipin.controller;

import com.caipin.domain.table.Classroom;
import com.caipin.domain.web.PageBean;
import com.caipin.domain.web.ResultJson;
import com.caipin.service.ClbumManagementService;
import com.caipin.utils.ImportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2020-07-29 15:35
 * @Description: TODO
 */
@RestController
@RequestMapping({"/clbumManagement"})
public class ClbumManagementController {
    @Autowired
    private ClbumManagementService clbumManagementService;

    @PostMapping({"/classroomImport"})
    public Object classroomImport(@RequestParam("classroom") MultipartFile file, HttpServletRequest request) throws IOException {
        InputStream inputStream = file.getInputStream();
        List<Classroom> classroomList = ImportExcel.importExcel(inputStream, 1, 0, Classroom.class);
        int count = this.clbumManagementService.importClassroomToMysql(classroomList);
        Map<String, Integer> map = new HashMap<>();
        map.put("count", Integer.valueOf(count));
        return ResultJson.ok(map);
    }

    @GetMapping({"/classroomList"})
    public Object classroomList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, @RequestParam(value = "buildingName", required = false) String buildingName, @RequestParam(value = "classroomId", required = false) String classroomId, @RequestParam(value = "classroomName", required = false) String classroomName, @RequestParam(value = "grade", required = false) String grade, @RequestParam(value = "clbum", required = false) String clbum, @RequestParam(value = "teacherId", required = false) String teacherId, @RequestParam(value = "teacherName", required = false) String teacherName) {
        PageBean<Classroom> pageBean = null;
        if (buildingName == null && classroomId == null && classroomName == null && grade == null && clbum == null && teacherId == null && teacherName == null) {
            pageBean = this.clbumManagementService.findClassroomList(pageNum, pageSize);
        } else {
            pageBean = this.clbumManagementService.findClassroomList(pageNum, pageSize, buildingName, classroomId, classroomName, grade, clbum, teacherId, teacherName);
        }
        return ResultJson.ok(pageBean);
    }

    @GetMapping({"/getClassroom/{id}"})
    public Object getClassroom(@PathVariable("id") Integer id) {
        Classroom classroom = this.clbumManagementService.findClassroomById(id);
        return ResultJson.ok(classroom);
    }

    @DeleteMapping({"/delClassroom/{id}"})
    public Object delClassroom(@PathVariable("id") Integer id) {
        this.clbumManagementService.delClassroomById(id);
        return ResultJson.ok();
    }

    @DeleteMapping({"/delClassroomList"})
    public Object delClassroomList(@RequestBody List<Integer> ids) {
        int count = this.clbumManagementService.delClassroomListByIds(ids);
        Map<String, Integer> map = new HashMap<>();
        map.put("count", Integer.valueOf(count));
        return ResultJson.ok(map);
    }

    @PostMapping({"/addClassroom"})
    public Object addClassroom(@RequestBody Classroom classroom) {
        this.clbumManagementService.addClassroom(classroom);
        return ResultJson.ok();
    }

    @PutMapping({"/modifyClassroom"})
    public Object modifyClassroom(@RequestBody Classroom classroom) {
        this.clbumManagementService.updateClassroom(classroom);
        return ResultJson.ok();
    }
}
